package cn.dankal.flyaudio.personal.driverlisense

import androidx.core.content.ContextCompat
import android.view.View
import android.widget.TextView
import cn.dankal.basiclib.base.BaseRvActivity
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener
import cn.dankal.basiclib.protocol.MainProtocol.KEY_BEAN
import cn.dankal.basiclib.protocol.PersonalProtocol
import cn.dankal.basiclib.widget.titlebar.TitleWithAction
import cn.dankal.business.eventbus.MessageDriverHomeEvent
import cn.dankal.business.eventbus.MessageUpdateCarListEvent
import cn.dankal.business.pojo.LisenseBean
import cn.dankal.flyaudio.R
import com.alibaba.android.arouter.launcher.ARouter
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class DriverLisenseActivity : BaseRvActivity<LisenseBean>() {

    override fun getPresenter(): BaseRecyclerViewPresenter<LisenseBean> {
        return LisensePresenter()
    }

    override fun getAdapter(): BaseRecyclerViewAdapter<LisenseBean> {
        var lisenseAdapter = LisenseAdapter()
        lisenseAdapter.setOnRvItemClickListener(object :OnRvItemClickListener<LisenseBean>(){
            override fun onItemClick(v: View?, position: Int, data: LisenseBean?) {
                ARouter.getInstance().build(PersonalProtocol.VERIFY)
                        .withSerializable(KEY_BEAN,data)
                        .navigation()
            }
        })
        return lisenseAdapter
    }

    override fun initComponents() {
        super.initComponents()
        var titleWithAction = TitleWithAction("车主认证列表")
        addTitleBar(titleWithAction)

        var titleView = titleWithAction.titleView.findViewById<TextView>(R.id.tv_action)
        titleView.text="添加认证"
        titleView.setOnClickListener {
            ARouter.getInstance().build(PersonalProtocol.VERIFY)
                    .navigation()
        }
        titleView.setCompoundDrawables(null,null,null,null)
        titleView.setTextColor(ContextCompat.getColor(applicationContext,R.color.color14))

    }

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onMessageEvent(event: MessageUpdateCarListEvent) {
        if (mPresenter != null)
            mPresenter.requestData(pageIndex.toString())
        mAdapter.notifyDataSetChanged()
    }

}
